<template>
  <div>
    <FilterTable
      ref="table"
      height="450"
      :columns="columns"
      :fetchapi="fetchApi"
      :isSelectColumn="isSelectColumn"
      :params="fetchParams"
      :datakey="'records'"
      columnsRef="customerManage_continueManage_record_followInfo_01"
      :isPagination="isPagination"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <el-button
          size="small"
          v-if="projectFollowUpStatus !== 25811006 && projectFollowUpStatus !== 25811007 && projectFollowUpStatus !== 25811008"
          type="primary"
          icon="el-icon-plus"
          @click="productHandle"
          >添加</el-button
        >
      </template>
    </FilterTable>

    <!-- 编辑 -->
    <BaseDialog title="维护跟进历史信息" width="700px" destroyOnClose :visible.sync="visibleFollowInfo" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <followInfoDlg ref="followInfoDlg" :type="handleType" :rowData="formData" @close="closeFollowInfo" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { followInfo } from '@/api/cms-api/customerManage/continueManage/record/index';
import { addCarInfo } from '@/api/cms-api/customerManage/continueManage/record/index';
import { delCarInfo } from '@/api/cms-api/customerManage/continueManage/record/index';
import { delFollow } from '@/api/cms-api/customerManage/continueManage/record/index';
import followInfoDlg from '@/pages/cms-pages/customerManage/continueManage/record/followEdit';
import { formatdateByTime, formatdateByDay } from '@/utils/index';
export default {
  mixins: [dictionary],
  components: {
    followInfoDlg
  },
  props: {
    value: {
      type: Number,
      required: true
    },
    projectFollowUpStatus: {
      type: Number,
      required: true
    },
    infoBasic: {
      type: String,
      required: true
    },
    refreshfun: {
      type: Function,
      default: null
    }
  },
  data() {
    return {
      isSelectColumn: false,
      flterValue: {}, //参数
      fetchApi: followInfo, //ipa 接口
      isPagination: false,
      visibleFollowInfo: false,
      formData: {},
      rowq: { projectId: this.value },
      handleType: '',
      columns: this.creatHeader(),
      fetchParams: { projectId: this.value } //ipa 接口
    };
  },
  created() {
    this.init();
  },
  methods: {
    formatDates: function(value) {
      // 这里的 value 就是需要过滤的数据
      if (value == null || value == '') {
        return '';
      }
      var date = new Date(value);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      return year + '-' + month + '-' + day;
    },
    //关闭详情
    closeFollowInfo(val, type) {
      this.visibleFollowInfo = false;
      if (type === 'refresh') {
        this.fetchParams = { ...this.fetchParams };
        this.refreshfun();
      }
    },
    editHandle(row) {
      console.log(row);
      this.formData = row;
      this.handleType = 'edit';
      this.visibleFollowInfo = true;
    },
    async del(followId) {
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          this.res = await delFollow(followId);
          console.log('232323232323-----------', this.res);
          if (this.res.resultCode != 200) {
            this.$notify.error({ title: '提示信息', message: res.errMsg || '删除失败!' });
          } else {
            this.$notify.success({ title: '提示信息', message: '删除成功!' });
            // 重新加载
            this.fetchParams = { ...this.fetchParams };
          }
        })
        .catch(() => {
          // this.$notify.info({title: '提示信息',message:'已取消删除'});
        });

      // this.res = await delFollow(followId);
      // this.fetchParams = { ...this.fetchParams };
    },
    deleHandle(followId) {
      this.del(followId);
    },
    async saveDialog(data, projectId) {
      this.res = await addCarInfo(data, projectId);
      this.fetchParams = { ...this.fetchParams }; //刷新
      this.visibleProduct = false; //测试
    },
    closeProduct(data) {
      console.log(data);
      this.saveDialog(data.data, this.value);
    },
    productHandle() {
      this.handleType = 'add';
      this.formData = this.rowq;
      this.visibleFollowInfo = true;
    },
    //初始化数据
    init() {
      console.log('行id---->', this.value);
      console.log('是否编辑---->', this.infoBasic);
      //赋值
      // this.initForm.forEach(x => {
      //     x.initialValue = data.row[x.fieldName];
      // });
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: props => {
            <el-button size="mini" type="text" onClick={() => this.deleHandle(props.row.followId)}>
              删除
            </el-button>;
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '跟进日期',
          dataIndex: 'followDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          render: props => {
            return <span>{formatdateByDay(props.row.followDate)} </span>;
          }
        },
        { title: '跟进人', dataIndex: 'followPeople', sorter: true },
        {
          title: '跟进方式',
          dataIndex: 'followType',
          sorter: true,
          render: props => {
            //编码转汉字
            return <span>{this.createDictText(props.row.followType, '2519')}</span>;
          }
        },
        {
          title: '跟进状态',
          dataIndex: 'followStatus',
          sorter: true,
          render: props => {
            //编码转汉字
            return <span>{this.createDictText(props.row.followStatus, '2520')}</span>;
          }
        },
        {
          title: '客户状态',
          dataIndex: 'customerStatus',
          sorter: true,
          render: props => {
            //编码转汉字
            return <span>{this.createDictText(props.row.customerStatus, '2507')}</span>;
          }
        },
        { title: '会见人', dataIndex: 'meetingPeople', sorter: true },
        //  { title: '同行人', dataIndex: 'togetherPeople', sorter: true },
        {
          title: '客户关系进度',
          dataIndex: 'relationProgress',
          sorter: true,
          render: props => {
            //编码转汉字
            return <span>{this.createDictText(props.row.relationProgress, '2521')}</span>;
          }
        },
        { title: '成交机会率(%)', dataIndex: 'dealRate', sorter: true },
        {
          title: '预计下次跟进日期',
          dataIndex: 'planNextDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          render: props => {
            return <span>{formatdateByDay(props.row.planNextDate)} </span>;
          }
        },
        { title: '输出成果', dataIndex: 'outputResult', sorter: true },
        { title: '下次目标', dataIndex: 'nextTarget', sorter: true },
        { title: '备注', dataIndex: 'remark', sorter: true }
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
